<template>
  <el-dialog :title="title" v-model="visible" width="780px" append-to-body>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="公告标题" prop="noticeTitle">
            <el-input v-model="form.noticeTitle" placeholder="请输入公告标题" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公告类型" prop="noticeType">
            <el-select v-model="form.noticeType" placeholder="请选择">
              <el-option
                v-for="dict in sys_notice_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in sys_notice_status"
                :key="dict.value"
                :value="dict.value"
              >{{ dict.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="内容">
            <editor v-model="form.noticeContent" :min-height="192"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import { useDict } from '@/utils/dict'
import { getNotice } from "@/api/system/notice";

const { sys_notice_status, sys_notice_type } = useDict("sys_notice_status", "sys_notice_type")

const visible = ref(false)
const title = ref('')
const form = ref({
  noticeId: undefined,
  noticeTitle: undefined,
  noticeType: undefined,
  noticeContent: undefined,
  status: "0"
})

const rules = ref({
  noticeTitle: [{ required: true, message: "公告标题不能为空", trigger: "blur" }],
  noticeType: [{ required: true, message: "公告类型不能为空", trigger: "change" }]
})

const emit = defineEmits(['submit'])

const open = (type, noticeId) => {
  if (type === 'add') {
    title.value = '添加公告'
    form.value = {
      noticeId: undefined,
      noticeTitle: undefined,
      noticeType: undefined,
      noticeContent: undefined,
      status: "0"
    }
    visible.value = true
  } else {
    getNotice(noticeId).then(response => {
      title.value = '修改公告'
      form.value = response.data
      visible.value = true
    })
  }
}

const cancel = () => {
  visible.value = false
}

const submitForm = () => {
  emit('submit', form.value)
  visible.value = false
}

defineExpose({
  open
})
</script>
